/*
 * @Description: 请描述此文件
 * @Author: LDM
 * @Date: 2021-09-02 19:13:44
 * @LastEditors: LDM
 * @LastEditTime: 2021-09-13 10:53:41
 */
import type { UserConfig, ConfigEnv } from 'vite'
import { defineConfig } from 'vite'
import { createVitePlugins } from './build/vite/plugins'
import { createDefineConst } from './build/vite/define'
import { resolve } from 'path'
import { createViteProxy } from './build/vite/proxy'

function pathResolve(dir: string) {
  return resolve(process.cwd(), '.', dir)
}

// https://vitejs.dev/config/
//eslint-disable-next-line
export default defineConfig(({ command, mode }: ConfigEnv): UserConfig => {
  console.log('%c 🍍 mode: ', 'font-size:20px;background-color: #33A5FF;color:#fff;', mode)
  // const root = process.cwd();

  const isBuild = command === 'build'

  return {
    root: process.cwd(),
    base: '/',
    plugins: createVitePlugins(isBuild),
    resolve: {
      alias: [
        {
          find: /\/@\//,
          replacement: pathResolve('src') + '/'
        },
        // /#/xxxx => types/xxxx
        {
          find: /\/#\//,
          replacement: pathResolve('types') + '/'
        }
        // ['@vue/compiler-sfc', '@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js'],
      ]
    },
    server: {
      open: true,
      // Listening on all local IPs
      host: true,
      hmr: {
        overlay: true
      },
      // port: VITE_PORT,
      // Load proxy configuration from .env
      proxy: createViteProxy()
    },
    build: {
      target: 'es2015',
      outDir: 'dist',
      terserOptions: {
        compress: {
          keep_infinity: true,
          // Used to delete console in production environment
          drop_console: true
        }
      },
      // Turning off brotliSize display can slightly reduce packaging time
      brotliSize: false,
      chunkSizeWarningLimit: 2000
    },
    define: createDefineConst(isBuild),
    // define: {
    //   __BASE_API__: JSON.stringify('/api'),
    //   'process.env.ADMIN': JSON.stringify('admin')
    // },

    // The vite plugin used by the project. The quantity is large, so it is separately extracted and managed
    // plugins: createVitePlugins(viteEnv, isBuild),

    optimizeDeps: {}
  }
})
